package com.zly.android.aidl.sample

import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.content.ServiceConnection
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.IBinder
import android.widget.TextView

/**
 * 此时自己作为客户端去绑定service服务
 */
class MainActivity : AppCompatActivity() {
    var iRemoteService:IRemoteService?=null
    val mConnection=object :ServiceConnection{
        override fun onServiceConnected(name: ComponentName?, service: IBinder?) {
            iRemoteService=IRemoteService.Stub.asInterface(service)
            iRemoteService?.basicTypes(12,123,true
            ,12f,123.45,"服务端你好")

        }

        override fun onServiceDisconnected(name: ComponentName?) {
            iRemoteService=null
        }

    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        findViewById<TextView>(R.id.bind_service_tv).apply {
            setOnClickListener {
                if (text=="绑定服务"){
                    //绑定服务
                    val intent=Intent()
                    intent.action="com.zly.android.aidl"
                    intent.`package`="com.zly.android.aidl.sample"
                    bindService(intent,mConnection, Context.BIND_AUTO_CREATE)
                }else{
                    unbindService(mConnection)
                }
            }


        }

    }
}